package groups

import (
	"github.com/beego/beego/v2/client/orm"
	"xpms/app/models"
	"xpms/cores"
	"xpms/cores/utils"
)

type GroupsPermission struct {
	Id           int64 `orm:"pk;" json:"Id,string"`
	Groupid      int64 `json:"Groupid,string"`
	Permissionid int64 `json:"Permissionid,string"`
}

func (this *GroupsPermission) TableName() string {
	return models.TableName("groups_permission")
}

func init() {
	orm.RegisterModel(new(GroupsPermission))
}

func AddGroupsPermission(upd GroupsPermission) error {
	o := orm.NewOrm()
	to, err := o.Begin()
	permission := new(GroupsPermission)
	permission.Id = upd.Id
	permission.Groupid = upd.Groupid
	permission.Permissionid = upd.Permissionid
	_, err = to.Insert(permission)
	if err != nil {
		to.Rollback()
	} else {
		to.Commit()
	}

	return err
}

/**
  批量添加 组权限，添加前先删除组的权限 在添加
*/
func AddGroupsPermissions(groupid string, upd []GroupsPermission) error {
	o := orm.NewOrm()
	to, err := o.Begin()
	to.Raw("DELETE FROM "+models.TableName("groups_permission")+" WHERE groupid = ?", groupid).Exec()
	_, err = o.InsertMulti(100, upd)
	if err != nil {
		to.Rollback()
	} else {
		to.Commit()
	}
	return err
}

func DeleteGroupsPermission(id int64) error {
	o := orm.NewOrm()
	_, err := o.Raw("DELETE FROM "+models.TableName("groups_permission")+" WHERE id = ?", id).Exec()
	return err
}
func DeleteGroupsPermissionForGroupid(groupid int64) error {
	o := orm.NewOrm()
	_, err := o.Raw("DELETE FROM "+models.TableName("groups_permission")+" WHERE groupid = ?", groupid).Exec()
	return err
}

func ListGroupsPermission(groupid int64) (ops []GroupsPermission) {
	var permissions []GroupsPermission
	o := orm.NewOrm()
	qs := o.QueryTable(models.TableName("groups_permission"))
	cond := orm.NewCondition()
	cond = cond.And("groupid", groupid)
	qs = qs.SetCond(cond)
	qs.All(&permissions)

	return permissions
}

type GroupsUserPermission struct {
	Permissionid string
	Name         string
	Ename        string
	Icon         string
	Parentid     string
	Nav          string
	Type         string
	Method       string
}

func ListGroupsUserPermission(groupid []string) (num int64, err error, ops []GroupsUserPermission) {
	var users []GroupsUserPermission
	// nav 1 菜单 2 接口 3按钮
	sql := `
	SELECT
DISTINCT
	pp.name,
		pp.ename,
		pp.icon,
		pp.parentid,
		pp.nav,
		pp.type
		FROM
	pms_groups_permission AS gp
	LEFT JOIN pms_permissions AS p ON
	gp.permissionid = p.permissionid
	RIGHT JOIN pms_permissions AS pp ON
	pp.permissionid = p.parentid
	WHERE 1=1
	`
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(groupid) {
		sql = sql + ` and  gp.groupid in(`
		sql1, value := cores.SqlInParam(groupid)
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	//sql = sql + ` ) `
	o := orm.NewOrm()
	nums, err := o.Raw(sql, param).QueryRows(&users)
	// 获取菜单的sql

	return nums, err, users
}

/**
  查询 后端rest接口,权限
*/

func QueryGroupsRestPermission(condArr map[string]interface{}) (num int64, err error, ops []GroupsUserPermission) {
	var users []GroupsUserPermission
	sql := `
		SELECT
			DISTINCT  p.permissionid,
            p.name,
			p.ename,
			p.icon,
			p.parentid,
			p.nav,
			p.type,
            p.Method
		FROM pms_permissions p where  1=1 `
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(condArr["nav"]) {
		sql = sql + ` and  p.nav in(`
		sql1, value := cores.SqlInParam(condArr["nav"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	sql = sql + `and exists(select 1 from  pms_groups_permission gp where gp.permissionid=p.permissionid`

	if !utils.IsEmpty(condArr["groupid"]) {
		sql = sql + ` and  gp.groupid in(`
		sql1, value := cores.SqlInParam(condArr["groupid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	sql = sql + ` ) `
	if !utils.IsEmpty(condArr["isopen"]) {
		sql = sql + ` and  p.isopen in(`
		sql1, value := cores.SqlInParam(condArr["isopen"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	o := orm.NewOrm()
	nums, err := o.Raw(sql, param).QueryRows(&users)
	return nums, err, users
}
